<template>
    <div class="flash-sale">
      <div class="header">
        <h2 class="section-title">特价补贴</h2>
        <div class="countdown">
          <span class="countdown-title">距结束</span>
          <span class="countdown-item">{{ hours }}</span>:
          <span class="countdown-item">{{ minutes }}</span>:
          <span class="countdown-item">{{ seconds }}</span>
        </div>
      </div>
      
      <div class="product-list">
        <div 
          v-for="product in products" 
          :key="product.id" 
          class="product-card"
        >
          <img :src="product.productMainImg" :alt="product.productName" class="product-image" />
          <div class="product-info">
            <h3 class="product-name">{{ product.productName }}</h3>
            <div class="price-section">
              <span class="current-price">¥{{ product.productPrice }}</span>
              <span class="original-price">¥{{ (product.productPrice * 1.2).toFixed(1) }}</span>
            </div>
            <div class="sold-progress">
              <div class="progress-bar" :style="{ width: product.soldPercent + '%' }"></div>
              <span class="sold-text">已售{{ (product.productSold / product.productCount * 100).toFixed(1) }}%</span>
            </div>
            <!-- <button class="buy-btn" @click="addToCart(product)">立即抢购</button> -->
            <button class="buy-btn" @click="handleBuy(product)">立即抢购</button>

            <!-- <el-button 
              type="primary" 
              size="large" 
              @click="handleBuy"
            >
              立即购买
            </el-button> -->

          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, computed, onMounted, onUnmounted } from 'vue'
  import { useCartStore } from '@/stores/cartStore'
  import { productAPI } from '@/http'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'

const router = useRouter()

const handleBuy = (product) => {
  // 将商品数据通过路由state传递
  router.push({
    name: 'purchasedialog',
    query: {
      product: JSON.stringify(product)
    }
  })
}

  const cartStore = useCartStore()
  
  const now = ref(Date.now())
  const endTime = ref(Math.ceil(now.value / (60 * 60 * 1000)) * 60 * 60 * 1000)

  // 接收后端数据的变量
  const products = ref([
    {
      "id": 1027,
      "productName": "",
      "productBrand": "",
      "productType": "",
      "productPrice": 0,
      "productColor": "",
      "productSize": "",
      "productMainImg": "",
      "productCount": 0,
      "productPoint": 0,
      "productArea": "",
      "productSold": 0,
      "xx": "0",
      "yy": null,
      "productVersion": null,
      "productStatus": null
    }
  ])

  // 接收后端数据的函数
  const fetcheProduct = async () => {
    const response = await productAPI.getRandomProduct(4);
    products.value = response.data;
    console.log(products.value);
  }
  
  const timeLeft = computed(() => endTime.value - now.value)
  
  const hours = computed(() => {
    return Math.floor(timeLeft.value / (1000 * 60 * 60)).toString().padStart(2, '0')
  })
  
  const minutes = computed(() => {
    return Math.floor((timeLeft.value % (1000 * 60 * 60)) / (1000 * 60)).toString().padStart(2, '0')
  })
  
  const seconds = computed(() => {
    return Math.floor((timeLeft.value % (1000 * 60)) / 1000).toString().padStart(2, '0')
  })
  
  let timer = null
  
  onMounted(() => {
    timer = setInterval(() => {
      now.value = Date.now()
    }, 1000)
  })
  
  // 页面刷新时重新获取数据
  onMounted(() =>
    fetcheProduct()
  )
  
  onUnmounted(() => {
    clearInterval(timer)
  })
  
  const addToCart = (product) => {
    cartStore.addToCart(product)
  }
  </script>
  
  <style scoped>
  .flash-sale {
    margin-bottom: 40px;
  }
  
  .header {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
  }
  
  .countdown {
    display: flex;
    align-items: center;
    margin-left: 20px;
    color: #ff6700;
    font-size: 16px;
  }
  
  .countdown-title {
    margin-right: 10px;
  }
  
  .countdown-item {
    display: inline-block;
    min-width: 22px;
    height: 22px;
    line-height: 22px;
    text-align: center;
    background: #333;
    color: #fff;
    border-radius: 2px;
    margin: 0 3px;
    padding: 0 3px;
  }
  
  .product-list {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
  }
  
  .product-card {
    border: #ff9000 0.5px solid;
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
    transition: all 0.3s;
  }
  
  .product-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  }
  
  .product-image {
    width: 100%;
    height: 180px;
    object-fit: cover;
  }
  
  .product-info {
    padding: 15px;
  }
  
  .product-name {
    font-size: 16px;
    margin: 0;
    height: 40px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  
  .price-section {
    margin-bottom: 10px;
  }
  
  .current-price {
    font-size: 20px;
    color: #ff6700;
    font-weight: bold;
    margin-right: 10px;
  }
  
  .original-price {
    font-size: 14px;
    color: #999;
    text-decoration: line-through;
  }
  
  .sold-progress {
    height: 20px;
    background: #f5f5f5;
    border-radius: 10px;
    margin-bottom: 15px;
    position: relative;
  }
  
  .progress-bar {
    height: 100%;
    background: linear-gradient(to right, #ff9000, #ff6700);
    border-radius: 10px;
  }
  
  .sold-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 12px;
    color: #fff;
  }
  
  .buy-btn {
    width: 100%;
    padding: 8px 0;
    background: #ff6700;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s;
  }
  
  .buy-btn:hover {
    background: #ff4500;
  }
  </style>